/**
 * 
 */
package com.et114.components.acegi.intercept.web;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.acegisecurity.ConfigAttributeDefinition;
import org.acegisecurity.ConfigAttributeEditor;
import org.acegisecurity.intercept.web.AbstractFilterInvocationDefinitionSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

import com.et114.components.acegi.Constants;
import com.et114.core.utility.StringPool;

/*
 * @author guanhw
 */
public class DBFilterInvocationDefinitionSource4Resources extends
		AbstractFilterInvocationDefinitionSource {
	private static final Log		logger									= LogFactory.getLog( DBFilterInvocationDefinitionSource.class );
	private boolean					convertUrlToLowercaseBeforeComparison	= false;
	private boolean					useAntPath								= false;
	private final PathMatcher		pathMatcher								= new AntPathMatcher();
	private final PatternMatcher	matcher									= new Perl5Matcher();
	
	public ConfigAttributeDefinition lookupAttributes ( String url ) {
		try {
			List urls  = null;
			// 倒叙排序
			Collections.sort( urls );
			Collections.reverse( urls );
			if ( convertUrlToLowercaseBeforeComparison ) {
				url = url.toLowerCase();
			}

		
			String resString = null ; 
			//@since  v1.11
	
				for ( Iterator iterator = urls.iterator(); iterator.hasNext(); ) {
					resString = ( String ) iterator.next();
					java.util.StringTokenizer sts = new java.util.StringTokenizer( resString , Constants.URL_RESOURCES_SPLIT ) ;
					int count = sts.countTokens ( ) ; 
					boolean  matched = false ;
					for ( int i = 0 ; i < count ; i ++  ) {
						String rs = sts.nextToken ( ) ; 
						if ( isUseAntPath() ) {
							if ( rs .indexOf ( StringPool.COLON ) > 0 ) {
								rs = rs.substring ( rs.indexOf ( StringPool.COLON  ) + 1 , rs.length ( ) ) ; 
							}
							matched = pathMatcher.match( rs , url );
							
						} else {
							Pattern compiledPattern = null;
							Perl5Compiler compiler = new Perl5Compiler();
							try {
								compiledPattern = compiler.compile( rs , Perl5Compiler.READ_ONLY_MASK );
							} catch ( Exception mpe ) {
								logger.error( mpe.getMessage(), mpe );
							}
							matched = matcher.matches( url, compiledPattern );
						}
	
								
					}
					
					if ( matched ) break ;
				}

	
		
			if ( resString != null ) {
					ConfigAttributeEditor configAttrEditor = new ConfigAttributeEditor();
					configAttrEditor.setAsText( resString.trim() );
					return ( ConfigAttributeDefinition ) configAttrEditor.getValue();
			}

		} catch ( Exception e ) {
			logger.error( e.getMessage(), e );
		}
		return null;
	}
	
	public Iterator getConfigAttributeDefinitions ( ) {
		// TODO Auto-generated method stub
		return null;
	}
	

	public void setConvertUrlToLowercaseBeforeComparison (
			boolean convertUrlToLowercaseBeforeComparison ) {
		this.convertUrlToLowercaseBeforeComparison = convertUrlToLowercaseBeforeComparison;
	}

	public boolean isConvertUrlToLowercaseBeforeComparison ( ) {
		return convertUrlToLowercaseBeforeComparison;
	}

	public boolean isUseAntPath ( ) {
		return useAntPath;
	}

	public void setUseAntPath ( boolean useAntPath ) {
		this.useAntPath = useAntPath;
	}	
}
